package com.xml.utils;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Date;  

import com.xml.domain.Book;
import com.xml.domain.Borrow;
import com.xml.domain.Reader;
import com.xml.services.BorrowService;
import com.xml.services.BorrowServiceImpl;

public class test {
	
	

	
	@Test
	public void test(){
		Session session = HibernateUtils.getSession();
		Transaction transaction = session.beginTransaction();
		Book book = (Book) session.get(Book.class,1);
		transaction.commit();
		session.close();
		System.out.println(book);
		
	}
	
	
	@Test
	public void insertReader(){
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		Reader reader1 = new Reader();
		reader1.setReaderName("李四");
		reader1.setTelephone("922");
		
		session.save(reader1);
		
		session.getTransaction().commit();
		session.close();
		
	}
	
	@Test
	public void insertBook(){
		Integer a = new Integer(119);
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		Book book1 = new Book();
		book1.setIsbn("0013");
		book1.setBookName("英语");
		book1.setAuthor("史蒂夫");
		book1.setPrice(a);
		
		session.save(book1);
		
		session.getTransaction().commit();
		session.close();
	}
	
	@Test
	public void setReaderId(){
		Session session = HibernateUtils.getSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();
			Query query = session.createQuery("from Reader where readerId = ?");
			query.setParameter(0,"9999");
			Reader reader = (Reader) query.list().get(0);
			int id = reader.getId();
			String readerID = HibernateUtils.getSysYear()+id;
			while(readerID.length()<9){
				readerID = readerID+"0";
			}
			reader.setReaderId(readerID);
			session.update(reader);
			transaction.commit();
		}catch(Exception e){
			if(transaction!=null)
				transaction.rollback();
		}finally{
			session.close();
		}
		
		
	}
	@Test
	public void CheckReader(){
		try{
			Session session = HibernateUtils.getSession();
			Transaction transaction = session.beginTransaction();
			Query query = session.createQuery("from Reader where readerId = ?");
			query.setParameter(0,"201810000");
			Reader reader = (Reader) query.list().get(0);
			System.out.println(reader);
			
			transaction.commit();
			session.close();
		}catch (Exception e) {
			System.out.println("error");
		}
	}
	
	@Test
	public void borrowBook(){
		String readerId = "201910000";
		String isbn = "0001";
		Session session = HibernateUtils.getSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();
			Query query1 = session.createQuery("from Reader where readerId = ?");
			query1.setParameter(0,readerId);
			Reader reader = (Reader) query1.list().get(0);
			
			Query query2 = session.createQuery("from Book where isbn = ?");
			query2.setParameter(0,isbn);
			Book book = (Book) query2.list().get(0);
			System.out.println(reader);
			System.out.println(book);
			Borrow borrow = new Borrow();
			borrow.setBook(book);
			borrow.setReader(reader);
			session.save(borrow);
			
			reader.setNumOfBorrow(reader.getNumOfBorrow()+1);
			session.update(reader);
			
			book.setState(1);
			session.update(book);
			transaction.commit();
		}catch (Exception e) {
			if(transaction!=null){
				transaction.rollback();
			}
		}finally{
			session.close();
		}
	}
	
	@Test
	public void returnBook(){
		Borrow borrow = null;
		Session session = HibernateUtils.getSession();
		Transaction transaction = session.beginTransaction();
		
		String hql = "from Borrow b inner join fetch b.reader inner join fetch b.book where b.reader.readerId = ? and b.book.isbn = ?";
		
		Query query= session.createQuery(hql);
		query.setParameter(0, "201810000");
		query.setParameter(1, "0001");
		borrow = (Borrow) query.list().get(0);
		if(borrow==null){
			System.out.println("aaa");
		}
		
		System.out.println(borrow.getBook().getAuthor());
		
		transaction.commit();
		session.close();
	}
	
	@Test
	public void returnBooks(){
		String readerId = "201810000";
		String isbn = "0001";
		Borrow borrow = null;
		Session session = HibernateUtils.getSession();
		Transaction transaction = null;
		transaction = session.beginTransaction();
		String hql = "from Borrow b inner join fetch b.reader inner join fetch b.book where b.reader.readerId = ? and b.book.isbn = ?";
		Query query= session.createQuery(hql);
		query.setParameter(0, readerId);
		query.setParameter(1, isbn);
		borrow = (Borrow) query.list().get(0);
		System.out.println(borrow.getReader().getReaderName());
		transaction.commit();
		session.close();
	}
	
	
	@Test
	public void searchBorrowInfo(){
		Session session = HibernateUtils.getSession();
		Transaction transaction = null;
		transaction = session.beginTransaction();
		String hql = "from Borrow b inner join fetch b.reader inner join fetch b.book where b.reader.readerId = ?";
		Query query= session.createQuery(hql);
		query.setParameter(0, "201820000");
		List<Borrow> list = query.list();
		transaction.commit();
		session.close();
		for (Borrow borrow : list){
			System.out.println(borrow.getReader().getReaderName());
			System.out.println(borrow.getBook().getBookName());
			System.out.println(borrow.getBorrowTime());
			System.out.println("=========================");
		}
		
	}
	
	@Test
	public void demo3(){
		BorrowService bs = new BorrowServiceImpl();
		String readerId = "201810000";
		List<Borrow> list = bs.searchBorrowInfo(readerId);
		System.out.println(list.size());
		for(Borrow borrow : list){
			System.out.println(borrow.getReader().getReaderName());
			System.out.println(borrow.getBook().getBookName());
			System.out.println(borrow.getBorrowTime());
			System.out.println("=========================");
		}
	}
	
	@Test
	public void getTime(){
//		long l = System.currentTimeMillis();
		long l = System.currentTimeMillis();
		Date date = new Date(l);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println(dateFormat.format(date));
//		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	}

}
